# -*- coding: utf-8 -*-
#   各种不同样式的button
# @Time    ： 2023/10/23  11:38
# @Auther  ： 徐良
# File     :  buttons.py
style_emboss = """
    QPushButton {
        background: qlineargradient(
            x1:0, y1:0, x2:1, y2:1,
            stop:0 #F6EFEA,
            stop:0.45 #E8DAD1,
            stop:1 #D8C6B7
        );
        border: 1.8px solid #BCA797;
        border-radius: 8px;
        color: #6D4C41;
        padding: 7px 20px;
        font: 500 9.6pt "微软雅黑";
    }
    QPushButton:hover {
        background: qlineargradient(
            x1:0, y1:0, x2:1, y2:1,
            stop:0 #ECE1D9,
            stop:0.45 #DFD0C6,
            stop:1 #CDB9AB
        );
        border-color: #A78F7F;
        color: #5B3E34;
    }
    QPushButton:pressed {
        background: qlineargradient(
            x1:0, y1:0, x2:1, y2:1,
            stop:0 #C8A693,
            stop:0.45 #AE8A76,
            stop:1 #8A6A57
        );
        border-color: #7B5C4C;
        color: white;
    }
    QPushButton:disabled {
        background: #EFE7E1;
        border-color: #D6C8BF;
        color: #B3A59C;
    }
"""

from PySide6.QtWidgets import QPushButton, QGraphicsDropShadowEffect
from PySide6.QtGui import QColor, QEnterEvent
from PySide6.QtCore import QEasingCurve, QPropertyAnimation


class PushButton1(QPushButton):
    def __init__(self, parent=None):
        super(PushButton1, self).__init__(parent)


class PushButtonToolbox(QPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)

        # 阴影效果（永久存在，不会被删除）
        self._shadow = QGraphicsDropShadowEffect(self)
        self._shadow.setBlurRadius(18)
        self._shadow.setOffset(0, 2)
        self._shadow.setColor(QColor(0, 0, 0, 0))  # 初始阴影透明
        self.setGraphicsEffect(self._shadow)

        # 阴影动画（柔和浮动感）
        self._shadow_anim = QPropertyAnimation(self._shadow, b"color", self)
        self._shadow_anim.setDuration(200)
        self._shadow_anim.setEasingCurve(QEasingCurve.OutQuad)

        # 设置样式（你可换 flat / emboss）
        self.setStyleSheet(self.style_emboss_with_icon)

    def enterEvent(self, event: QEnterEvent):
        self._shadow_anim.stop()
        self._shadow_anim.setStartValue(self._shadow.color())
        self._shadow_anim.setEndValue(QColor(0, 0, 0, 55))   # 阴影淡淡浮起来
        self._shadow_anim.start()
        super().enterEvent(event)

    def leaveEvent(self, event):
        self._shadow_anim.stop()
        self._shadow_anim.setStartValue(self._shadow.color())
        self._shadow_anim.setEndValue(QColor(0, 0, 0, 0))    # 阴影淡出
        self._shadow_anim.start()
        super().leaveEvent(event)

    # 扁平柔和版样式
    style_flat = """
        QPushButton {
            background: #F4ECE5;
            border: 1.6px solid #C9B3A4;
            border-radius: 8px;
            color: #6D4C41;
            padding: 6px 20px;
            font: 500 9.6pt "微软雅黑";
        }
        QPushButton:hover {
            background: #E9DED6;
            border-color: #B89D8C;
            color: #5B3E34;
        }
        QPushButton:pressed {
            background: #B8947E;
            border-color: #7B5C4C;
            color: white;
        }
        QPushButton:disabled {
            background: #EFE7E1;
            border-color: #D6C8BF;
            color: #B3A59C;
        }
    """

    # 精致立体渐变版样式（可切换）
    style_emboss = """
        QPushButton {
            background: qlineargradient(
                x1:0, y1:0, x2:1, y2:1,
                stop:0 #F6EFEA,
                stop:0.45 #E8DAD1,
                stop:1 #D8C6B7
            );
            border: 1.8px solid #BCA797;
            border-radius: 8px;
            color: #6D4C41;
            padding: 7px 20px;
            font: 500 9.6pt "微软雅黑";
        }
        QPushButton:hover {
            background: qlineargradient(
                x1:0, y1:0, x2:1, y2:1,
                stop:0 #ECE1D9,
                stop:0.45 #DFD0C6,
                stop:1 #CDB9AB
            );
            border-color: #A78F7F;
            color: #5B3E34;
        }
        QPushButton:pressed {
            background: qlineargradient(
                x1:0, y1:0, x2:1, y2:1,
                stop:0 #C8A693,
                stop:0.45 #AE8A76,
                stop:1 #8A6A57
            );
            border-color: #7B5C4C;
            color: white;
        }
        QPushButton:disabled {
            background: #EFE7E1;
            border-color: #D6C8BF;
            color: #B3A59C;
        }
    """

    style_emboss_with_icon = """
    QPushButton {
        background: qlineargradient(
            x1:0, y1:0, x2:1, y2:1,
            stop:0 #F6EFEA,
            stop:0.45 #E8DAD1,
            stop:1 #D8C6B7
        );
        border: 1.6px solid #C9B3A4;
        border-radius: 9px;

        color: #6D4C41;
        font: 500 9pt "微软雅黑";

        padding-top: 6px;      /* 图标与文字垂直间距控制点 */
        padding-bottom: 4px;
    }

    QPushButton:hover {
        background: qlineargradient(
            x1:0, y1:0, x2:1, y2:1,
            stop:0 #ECE1D9,
            stop:0.45 #DFD0C6,
            stop:1 #CDB9AB
        );
        border-color: #A78F7F;
        color: #5B3E34;
    }

    QPushButton:pressed, QPushButton:checked {
        background: qlineargradient(
            x1:0, y1:0, x2:1, y2:1,
            stop:0 #C8A693,
            stop:0.45 #AE8A76,
            stop:1 #8A6A57
        );
        border-color: #7B5C4C;
        color: white;
        font-weight: 600;   /* 选中强调 */
    }

    QPushButton:disabled {
        background: #EFE7E1;
        border-color: #D6C8BF;
        color: #B3A59C;
    }
    """


class PushButtonToolBox(QPushButton):
    def __init__(self, parent=None):
        super(PushButtonToolBox, self).__init__(parent)
        self.setStyleSheet("""
        QPushButton {
                background-color: #3498db;
                border: 2px solid #3498db;
                color: white;
                padding: 3px 20px; /* 设置上下内边距为10px，左右内边距为20px */
                border-radius: 10px;
                font: 75 10pt "微软雅黑";
                text-align: left;
            }
            QPushButton:hover {
                background-color: #2374a8;
                border: 2px solid #2374a8;
            }
            QPushButton:pressed {
                background-color: #195f8e;
                border: 2px solid #195f8e;
            }
            """)


class TextButton(QPushButton):
    """纯文字圆形按钮：带 hover 和 pressed 反馈"""
    def __init__(self, text="", parent=None, size=80, color="green"):
        super().__init__(text, parent)
        self.size = size
        if color.lower() == "red":
            self.bg_color = "#e74c3c"
        elif color.lower() == "green":
            self.bg_color = "#2ecc71"
        else:
            raise ValueError("Color must be 'Red' or 'Green'")
        self.init_style()

    def init_style(self):
        self.setFixedSize(self.size, self.size)

        # 计算 hover 和 pressed 颜色
        hover_color = self.darken_color(self.bg_color, 0.2)   # 悬停稍暗
        pressed_color = self.darken_color(self.bg_color, 0.4) # 按下更暗

        style = f"""
            QPushButton {{
                border-radius: {self.size // 2}px;
                background-color: {self.bg_color};
                color: white;
                font-size: 14px;
                font-weight: bold;
                outline: none;
                border: none;
                padding: 0px;
                transition: background-color 0.15s ease, transform 0.15s ease;
                transform-origin: center;
            }}
            QPushButton:hover {{
                background-color: {hover_color};
                transform: scale(1.05);
            }}
            QPushButton:pressed {{
                background-color: {pressed_color};
                transform: scale(0.95);
            }}
        """
        self.setStyleSheet(style)

    @staticmethod
    def darken_color(color_hex, factor):
        """将十六进制颜色变暗（factor 越大越暗）"""
        r = int(color_hex[1:3], 16)
        g = int(color_hex[3:5], 16)
        b = int(color_hex[5:7], 16)
        r = max(0, int(r * (1 - factor)))
        g = max(0, int(g * (1 - factor)))
        b = max(0, int(b * (1 - factor)))
        return f"#{r:02x}{g:02x}{b:02x}"
